package web;


import com.alibaba.fastjson.JSON;
import pojo.PageBean;
import pojo.User;
import service.UserService;
import service.Uservice;
import service.impl.UserviceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet{
    private Uservice service = new UserviceImpl();
    private final UserService service1 = new UserService();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<User> users = service.selectuserAll();
        //2. 存入request域中
        String  jsonString=JSON.toJSONString(users);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }


    public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        User user= JSON.parseObject(params,User.class);
        String username=user.getUsersname();
        User user1 = service1.insertoff(username);
        if (user1!=null){
            response.getWriter().write("shibai");
            return;
        }
        service.addUser(user);
        response.getWriter().write("success");
    }

    public void selectLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username= request.getParameter("username");
        String password= request.getParameter("password");

        User user=new User();
        user.setUsersname(username);
        user.setPassword(password);
        User user1=service.selectLogin(user);
        if(user1==null){
            response.sendRedirect("http://localhost:8080/01_46_luowei_war/lesson6/login_fail.jsp");
        } else{
            request.getSession().setAttribute("user",user1);
            request.getRequestDispatcher( "http://localhost:8080/01_46_luowei_war/index.jsp").forward(request,response);
        }
    }
    public void deleteByid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        int id= Integer.parseInt(params);
        service.deleteByid(id);
        response.getWriter().write("success");
    }
    public void deleteByids(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        int[] ids= JSON.parseObject(params,int[].class);
        service.deleteByids(ids);
        response.getWriter().write("success");
    }
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受参数，当前页码，每页条数
        String _currentPage=request.getParameter("currentPage");
        String _pageSize =request.getParameter("pageSize");
        int currentPage=Integer.parseInt(_currentPage);
        int pageSize=Integer.parseInt(_pageSize);
        PageBean pageBean=service.selectByPage(currentPage,pageSize);
        //2. 存入request域中
        String  jsonString=JSON.toJSONString(pageBean);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受参数，当前页码，每页条数
        String _currentPage=request.getParameter("currentPage");
        String _pageSize =request.getParameter("pageSize");
        //获取查询条件对象
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        User user= JSON.parseObject(params,User.class);

        int currentPage=Integer.parseInt(_currentPage);
        int pageSize=Integer.parseInt(_pageSize);
        PageBean pageBean=service.selectByPageAndCondition(currentPage,pageSize,user);
        //2. 存入request域中
        String  jsonString=JSON.toJSONString(pageBean);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void updateplus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        User user= JSON.parseObject(params,User.class);
        int count= service.updateplus(user);

        response.getWriter().write("success");
    }
    public void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        int id= JSON.parseObject(params,int.class);
        User user= service.selectById(id);
        String  jsonString=JSON.toJSONString(user);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);

    }
    public void upplus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String usersname = request.getParameter("username");
        String name = request.getParameter("name");
        String passwd = request.getParameter("passwd");
        String sex = request.getParameter("sex");
        String work = request.getParameter("work");
        String interest = request.getParameter("interest");
        String remark = request.getParameter("remark");
        User user=new User();
        user.setId(id);
        user.setUsersname(usersname);
        user.setPassword(passwd);
        user.setName(name);
        user.setWork(work);
        user.setInterest(interest);
        user.setSex(sex);
        user.setRemark(remark);
        int count= service.updateplus(user);
        request.getSession().invalidate();
        response.sendRedirect("http://localhost:8080/01_46_luowei_war/index.jsp");
    }
}
